<template>
    <div class="contract">
        <ContentWrap class="left">
            <div class="card-header">
                <button @click="cancell">返回列表</button>
            </div>
            <div class="main">
                <h3 class="title"></h3>
                <div class="address">
                    <Icon class="mr-5px" icon="ep:location" />
                    {{todal.data.province+todal.data.city+'市'+todal.data.area+todal.data.address}}
                </div>
                <div class="imgurll">
                    <img class="imgs" :src="todal.data.pics" alt="">
                    <!-- <img class="imgs" src="../../../../assets/imgs/avatar.gif" alt="">
                    <img class="imgs" src="../../../../assets/imgs/avatar.gif" alt="">
                    <img src="../../../../assets/imgs/avatar.gif" alt=""> -->
                </div>
                <div class="main-tit">
                    <div class="main-content">
                        <p class="main-p">基本信息</p>
                        <div class="main-hez">
                            <div class="main-left">
                                <el-form :model="todal.data" label-width="80px">
                                    <el-form-item label="项目名称">
                                        <el-input v-model="todal.data.name" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="总包公司"  class="formBox">
                                         <el-select placeholder="请选择总包公司" v-model="todal.data.company" clearable>
                                            <el-option v-for="(item) in userList" :key="item.id" :label="item.nickname" :value="item.nickname" />
                                        </el-select>
                                        <!-- <el-input v-model="todal.data.company" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item label="地址    ">
                                        <el-input v-model="todal.data.address" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item  class="formBox" label="开始时间">
                                        <el-date-picker
                                        v-model="todal.data.startTime"
                                        type="date"
                                        placeholder="选择开始时间">
                                        </el-date-picker>
                                        <!-- <el-input v-model="todal.data.startTime" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item class="formBox" label="结束时间">
                                        <el-date-picker
                                        v-model="todal.data.endTime"
                                        type="date"
                                        placeholder="选择结束时间">
                                        </el-date-picker>
                                        <!-- <el-input v-model="todal.data.endTime" style="width: 333px;" /> -->
                                    </el-form-item>
                                    <el-form-item label="经度">
                                        <el-input v-model="todal.data.lat" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="维度">
                                        <el-input v-model="todal.data.lon" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="省">
                                        <el-input v-model="todal.data.province" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="市">
                                        <el-input v-model="todal.data.city" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="区">
                                        <el-input v-model="todal.data.area" style="width: 333px;" />
                                    </el-form-item>
                                    <el-form-item label="项目经理" class="formBox">
                                        <el-select v-model="todal.data.userId" placeholder="请选择项目经理">
                                            <el-option label="123" value="123" />
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="财务    " class="formBox">
                                        <el-select v-model="todal.data.financeId" placeholder="请选择财务">
                                            <el-option label="123" value="123" />
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="订单数量">
                                        <el-input v-model="todal.data.orderNumber" />
                                    </el-form-item>
                                    <el-form-item>
                                        <el-button @click="cancell">取消</el-button>
                                        <el-button type="primary" @click="saveFn">保存并网签</el-button>
                                    </el-form-item>
                                </el-form>
                            </div>
                            <!-- <div class="main-left">
                                <div class=" main-zb">
                                    <p>总包单位：</p><span>{{ todal.data.company }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>开始时间：</p><span>{{ todal.data.startTime }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>结束时间：</p><span>{{ todal.data.endTime }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>项目经理：</p><span>{{ todal.data.userId }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>财务：</p><span>{{ todal.data.financeId }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>订单总量：</p><span>{{ todal.data.orderNumber }}</span>
                                </div>
                                <div class="main-zb">
                                    <p>完成量：</p><span>500</span>
                                </div>
                                <div class="main-zb">
                                    <p>营收账款：</p><span>2000000万元</span>
                                </div>
                                <div class="main-zb">
                                    <p>实收账款：</p><span>10000000万元</span>
                                </div>
                                <div class="main-zb">
                                    <p>风险等级：</p><el-tag class="ml-2" type="danger" style="color: red;">高</el-tag>
                                </div>
                            </div> -->
                        </div>
                    </div>
                </div>
            </div>
        </ContentWrap>
        <ContentWrap class="right">
            <div id="map" class="map" v-show="!isMapLoadingDown"></div>
            <!-- <div class="map"  v-show="isMapLoadingDown">地图加载中...</div> -->
            <el-main v-loading="true" element-loading-text="地图加载中"  v-show="isMapLoadingDown" class="elmain"></el-main>
        </ContentWrap>
    </div>
</template>

<script setup lang="ts">

import { useRoute } from "vue-router";
import { getProject,updateProject } from "@/api/system/project/index"
import * as UserApi from '@/api/system/user'
const userList = ref([])
let isMapLoadingDown = ref(true)
const router = useRouter();
let locationArr = ref<number[]>([118.182547, 24.488449])

// 在loadMap方法的then里面调用初始化地图
const loadAMap = (lat,lng) => {
    fixedPos(lat,lng)
};
// 点击定位-定位到当前位置
const  fixedPos =(lat,lng)=> {
   
    isMapLoadingDown.value = false
    var map = document.getElementById("map")
    var pos = new qq.maps.LatLng(lat,lng)
    
    var mapInfo = new qq.maps.Map(map, {
        // 地图的中心地理坐标。
        center: pos,
        zoom:20
    });
     //创建一个Marker
     var marker = new qq.maps.Marker({
        //设置Marker的位置坐标
        position: pos,
        //设置显示Marker的地图
        map: mapInfo
    });
    marker.setVisible(true);
    //设置Marker的动画属性为从落下
    marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
    //设置Marker是否可以被拖拽，为true时可拖拽，false时不可拖拽，默认属性为false
    marker.setDraggable(true);
     //添加信息窗口
     var info = new qq.maps.InfoWindow({
        map: mapInfo
    });
    info.setPosition(marker.getPosition());
    //标记Marker点击事件
    qq.maps.event.addListener(marker, 'click', function() {
        info.setPosition(marker.getPosition());
    });
    //设置Marker停止拖动事件
    qq.maps.event.addListener(marker, 'dragend', function() {
        // info.open();
        console.log(marker.getPosition(),'11111155555')
        let pos = marker.getPosition()
        todal.data.lat = pos.lat
        todal.data.lon = pos.lng
        info.setPosition(marker.getPosition());
    });
   
}
const todal = reactive<any>({
    data:{

    }
})
const ids = ref()
const route = useRoute();
// console.log(route.query.id)
const getList = async () => {
    ids.value = route.query.id
    console.log(ids.value)
    let res = await getProject(ids.value)
    todal.data = res
    todal.data.startTime = dateChangeFn(todal.data.startTime)
    todal.data.endTime = dateChangeFn(todal.data.endTime)

    if(todal.data.pics){
        var str2 = todal.data.pics
        var index = str2.lastIndexOf("\/");  
        str2  = str2.substring(index + 1, str2.length);
        todal.data.pics = 'http://jc.layoxx.com'+'/images/'+str2+'.png'
    }
    let areaNumArr = []
    let lng = todal.data.lon.split(',')[0]
    let lat = todal.data.lat.split(',')[0]
    areaNumArr.push(lng)
    areaNumArr.push(lat)
    console.log(areaNumArr,todal.data,"1111")

   locationArr.value = areaNumArr

    loadAMap(lat,lng);
    // http://jc.layoxx.com/images/aa4e3894056e5acbf9e141cc2d8a21b14d75849e95a2c4e241ca502723132b01.png
}

console.log(todal.value, "nishishab")

const dateChangeFn=(times)=>{
  if(!times){
    return '暂无'
  }
  var date = new Date(times);  // 参数需要毫秒数，所以这里将秒数乘于 1000
  let Y = date.getFullYear() + '-';
   let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  let D = date.getDate() + ' ';
  // let h = date.getHours() + ':';
  // let m = date.getMinutes() + ':';
  // let s = date.getSeconds();
  return Y+M+D
}
// 取消
const cancell = () => {
    router.push('/system/project/project')
}
// 编辑保存
const saveFn = async()=>{
    let stime = Date.parse(todal.data.startTime)
    let etime = Date.parse(todal.data.endTime)
    let res = await updateProject({
        name:todal.data.name,
        id: todal.data.id,
        name: todal.data.name,
        pics: todal.data.pics,
        company: todal.data.company,
        province: todal.data.province,
        city: todal.data.city,
        area: todal.data.area,
        address: todal.data.address,
        lat: todal.data.lat,
        userId: todal.data.userId,
        financeId: todal.data.financeId,
        orderNumber: todal.data.orderNumber,
        lon: todal.data.lon,
        startTime: stime,
        endTime: etime

    })
    console.log(res)
    router.push('/system/project/project')
}
onMounted(async() => {
    getList()
     // 加载总包公司列表
    userList.value = await UserApi.getSimpleUserAllList()
})
</script>


<style lang="scss" scoped>
.contract {
    display: flex;
    width: 100%;
    height: 100%;

    .drop {
        width: 8px;
        height: 8px;
        border: 2px solid rgba(81, 123, 255, 1);
    }

    .left {
        border-radius: 20px;
        margin-right: 20px;
        height: 100%;

        .card-header {
            span {
                font-size: 12px;
            }
        }

        .main {
            .title {
                font-size: 22px;
                font-weight: 500;
                margin: 24px 0px;
            }

            .main-tit {
                .main-content {
                    margin-bottom: 7px;

                    .main-p {
                        font-size: 16px;
                        font-weight: 500;
                        margin: 20px 0px;
                    }

                    // 文字前面的点
                    .main-p::before {
                        content: "●";
                        display: inline-block;
                        width: 10px;
                        height: 10px;
                        margin-right: 10px;
                        color: rgba(81, 123, 255, 1);
                        ;
                    }


                    .main-hez {
                        display: flex;
                        border-radius: 16px;
                        background: rgba(249, 250, 255, 1);
                        padding: 24px;

                        .main-left {
                            .main-zb {
                                display: flex;

                                p {
                                    display: flex;
                                    width: 80px;
                                    font-size: 16px;
                                    font-weight: 500;
                                    color: rgba(0, 0, 0, 0.6);
                                    margin-bottom: 18px;
                                    justify-content: flex-start;

                                }

                                span {
                                    color: rgba(0, 0, 0, 1);
                                }
                            }


                        }

                        .main-right {
                            p {
                                margin: 10px 0px;
                            }
                        }
                    }
                }
            }
        }
    }

    .address {
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 0px;
        line-height: 20.27px;
        color: rgba(0, 0, 0, 0.4);
    }

    .imgurll {
        display: flex;

        img {
            width: 100px;
            height: 74px;
            border-radius: 10px;
        }

        .imgs {
            margin-right: 22px;
        }
    }

    .right {
        flex: 1;
        border-radius: 20px;

        #map {
            width: 100%;
            height: 1000px;
            border-radius: 20px;
        }
    }

}

::v-deep .amap-logo {
    opacity: 0 !important;
}

::v-deep .amap-copyright {
    opacity: 0 !important;
}
.formBox ::v-deep .el-input__wrapper{
    width: 333px !important;
}
.formBox ::v-deep .el-select{
    width: 333px !important;
}
</style>